//
//  HBFollowController.m
//  HeartBeat
//
//  Created by karong on 2023/8/28.
//

#import "HBFollowController.h"
#import "HBFollowItemController.h"
#import "HBHistoryController.h"
#import "UIView+WSDraw.h"

@interface HBFollowController () <HBFollowItemControllerDelegate, HBHistoryControllerDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btnArray;
@property (weak, nonatomic) IBOutlet UIButton *editBtn;
@property (nonatomic, strong) NSMutableDictionary *contentVCDict;
@property (nonatomic, assign) CGFloat contentHeight;
@property (nonatomic, strong) HBBaseController * currentVC;
@end

@implementation HBFollowController
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.font_yellow_FFCC00;
    [self viewLayout];
    [self setContentVCWithIndex:0];
}

- (void)viewLayout {
    [self.scrollView setContentSize:CGSizeMake(kScreenWidth * 2, self.contentHeight)];
}

- (UIViewController *)followItemController {
    UIViewController * target = [ViewManager followItemVCInController:self];
    target.view.frame = CGRectMake(0, 0, HBScreenWidth, self.contentHeight);
    return target;
}

- (UIViewController *)historyController {
    UIViewController * target = [ViewManager historyVCInController:self];
    target.view.frame = CGRectMake(HBScreenWidth, 0, HBScreenWidth, self.contentHeight);
    return target;
}

- (void)turn2Follow {
    [self setContentVCWithIndex:0];
}

// 获取並且添加子控制器
- (void)setContentVCWithIndex:(NSInteger)index {
    BOOL animated = YES;
    if(self.contentVCDict.count == 0) {
        animated = NO;
    }
    HBBaseController *vc = self.contentVCDict[@(index)];
    if(!vc) {
        if (index == 0) {
            vc = (HBBaseController *)self.followItemController;
        }
        if (index == 1) {
            vc = (HBBaseController *)self.historyController;
        }
        vc.view.frame = self.scrollView.bounds;
        vc.view.mj_x = HBScreenWidth * index;
        vc.view.mj_y = 0;
        [self.scrollView addSubview:vc.view];
        [self addChildViewController:vc];
        self.contentVCDict[@(index)] = vc;
    }
    if (index == 0 && self.currentVC != vc) {
        [HBEvenManager event:@"book_tracking" evenLogType:(HBEvenLogType_Pop)
                  attributes:@{@"page_view":SF(@"%ld", (long)index+1)}];
    }
    self.currentVC = vc;
    [self.scrollView setContentOffset:CGPointMake(kScreenWidth * index, 0) animated:animated];
}

- (void)setBtnStatusWithIndex:(NSInteger)index {
    for (UIButton * btn in self.btnArray) {
        btn.selected = btn.tag == index;
        btn.titleLabel.font = (btn.tag == index)?[UIFont systemFontOfSize:18 weight:(UIFontWeightMedium)]:[UIFont systemFontOfSize:15 weight:(UIFontWeightRegular)];
    }
}

- (IBAction)btnAction:(UIButton *)sender {
    if (!self.scrollView.scrollEnabled) {
        return;
    }
    [self setBtnStatusWithIndex:sender.tag];
    if (self.scrollView.scrollEnabled) {
        [self setContentVCWithIndex:sender.tag];
    }
}

- (IBAction)editAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.scrollView.scrollEnabled = !sender.selected;
    if (self.currentVC) {
        SEL sel;
        if (sender.selected) {
            sel = NSSelectorFromString(@"beginEdit");
        }
        else {
            sel = NSSelectorFromString(@"endEdit");
        }
        if ([self.currentVC respondsToSelector:sel]) {
            [self.currentVC performSelector:sel withObject:nil];
        }
    }
}
#pragma mark HBFollowItemControllerDelegate
- (void)followItemControllerExitEdit {
    self.scrollView.scrollEnabled = YES;
    self.editBtn.selected = NO;
}
#pragma mark HBHistoryControllerDelegate
- (void)historyControllerExitEdit {
    self.scrollView.scrollEnabled = YES;
    self.editBtn.selected = NO;
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        CGFloat x = scrollView.contentOffset.x / HBScreenWidth;
        [self setBtnStatusWithIndex:x];
        [self setContentVCWithIndex:x];
    }
}

- (NSMutableDictionary *)contentVCDict {
    if (!_contentVCDict) {
        _contentVCDict = [[NSMutableDictionary alloc]init];
    }
    return _contentVCDict;
}

- (CGFloat)contentHeight {
    if (_contentHeight <= 0) {
        _contentHeight = HBScreenHeight - self.view.safeAreaInsets.top - 20 - 65 - self.tabBarController.tabBar.frame.size.height;
    }
    return _contentHeight;
}
@end
